package databasetest;

import java.awt.EventQueue;
import java.sql.Connection;
import java.sql.PreparedStatement;

import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Insert extends JInternalFrame {
	
	Connection conn;				
	DatabaseTest dt;
	
	
	private JLabel lblNewLabel;
	private JLabel lblNewLabel_1;
	private JTextField code;
	private JTextField ea;
	private JButton btnNewButton;
	private JLabel status;
	



	/**
	 * Create the frame.
	 */
	public Insert(Connection c, DatabaseTest dt) {				// 만들고 밑에꺼 위로 보내고
		super("입력",true,true,true,false);
		setBounds(50, 50, 450, 300);
		getContentPane().setLayout(null);
		getContentPane().add(getLblNewLabel());
		getContentPane().add(getLblNewLabel_1());
		getContentPane().add(getCode());
		getContentPane().add(getEa());
		getContentPane().add(getBtnNewButton());
		getContentPane().add(getStatus());
		
		conn=c;
		this.dt=dt;
	}
	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("code");
			lblNewLabel.setBounds(26, 36, 57, 15);
		}
		return lblNewLabel;
	}
	private JLabel getLblNewLabel_1() {
		if (lblNewLabel_1 == null) {
			lblNewLabel_1 = new JLabel("ea");
			lblNewLabel_1.setBounds(26, 85, 57, 15);
		}
		return lblNewLabel_1;
	}
	private JTextField getCode() {
		if (code == null) {
			code = new JTextField();
			code.setBounds(109, 33, 116, 21);
			code.setColumns(10);
		}
		return code;
	}
	private JTextField getEa() {
		if (ea == null) {
			ea = new JTextField();
			ea.setBounds(109, 82, 116, 21);
			ea.setColumns(10);
		}
		return ea;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("입력");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					
					try{
						String c =code.getText();
						int e1=Integer.parseInt(ea.getText());
						
						String sql="insert into ptbl(serial,code,ea) values(ptbl_serial.nextval,?,?)" ;   //실행할 문장은 완성되어있지만 아직은 미완성...물음표
						PreparedStatement pstmt = conn.prepareStatement(sql);		
						
						pstmt.setString(1, c)  ;  // 숫자1은 첫번째 물음표를 나타낸다(위치가아님)
						pstmt.setInt(2, e1) ;		//  2번째꺼는 Int형임						
						pstmt.executeUpdate() ;  //입력수정삭제는  무조건 업데이트     조회?만 셀렉
						
						if(dt.select != null)  dt.select.setList();	
							
					}catch(Exception ex){
						ex.printStackTrace();
					}
					
					
				 
				}
			});
			btnNewButton.setBounds(66, 130, 97, 23);
		}
		return btnNewButton;
	}
	private JLabel getStatus() {
		if (status == null) {
			status = new JLabel("New label");
			status.setBounds(26, 206, 252, 21);
		}
		return status;
	}
}
